<template>
  <div class="login-form">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>黑马面经运营后台</span>
      </div>
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="用户名:" prop="username">
          <el-input type="text" placeholder="请输入用户名" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input  type="password" placeholder="请输入密码" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <div class="login-btn">
            <el-button type="primary" @click="login">登陆</el-button>
            <el-button @click="reset">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { mapActions} from 'vuex'
export default {
  name: "LoginIndex",
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: ["blur", "change"],
          },
          {
            min: 5,
            max: 11,
            message: "长度在 5 到 11 个字符",
            trigger: ["blur", "change"],
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^\w{5,11}$/,
            message: "请输入 5 到 10 位的密码",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods:{
    ...mapActions('use',['loginAction']),
    async login(){
      try {
        await this.$refs.form.validate()
        this.loginAction(this.form)
        await this.$router.push('/article')
        this.$message.success('登陆成功')
      } catch (e) {
        console.log('表单不合法',e)
      }
    },
    reset(){
      this.$refs.form.resetFields()
    }
  }
};
</script>

<style lang="scss" scoped>
.login-form {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: center;
  .el-card {
    width: 420px;
    margin: 80px auto;
  }
}
.login-btn {
  text-align: center;
}
::v-deep .el-card__header {
  background-color: #727cf6;
  text-align: center;
  span {
    color: white;
  }
}
</style>
